package com.pzhu.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pzhu.entity.Business;
import com.pzhu.entity.Code;
import com.pzhu.entity.Goods;
import com.pzhu.entity.Result;
import com.pzhu.service.GoodsService;
import com.pzhu.until.JwtTokenUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping("/addlist")
    public Result addList(HttpServletRequest request, @RequestBody List<Goods> goodsList) {
        goodsList.forEach(System.out::println);
        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();
        goodsList.forEach(item -> item.setGoodsBusinessId(loginBusiness));
        boolean save = goodsService.saveBatch(goodsList);
        return new Result(Code.success, "", save);
    }

    @PostMapping("/deletelist")
    public Result deleteList(@RequestBody List<Goods> goodsList) {
        boolean b = goodsService.removeBatchByIds(goodsList);
        return new Result(Code.success, "", b);
    }

    /**
     * 分页条件查询商品
     *
     * @param request
     * @param current
     * @param size
     * @param goodsName
     * @param goodsType
     * @return
     */
    @GetMapping
    public Result select(HttpServletRequest request, @RequestParam("current") Integer current, @RequestParam("size") Integer size, @RequestParam("goodsName") String goodsName, @RequestParam("goodsType") String goodsType) {


        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();
        LambdaQueryWrapper<Goods> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Goods> orderPage = new Page<>(current, size);

        orderLambdaQueryWrapper.eq(Goods::getGoodsBusinessId, loginBusiness);


        orderLambdaQueryWrapper.like(goodsName != null, Goods::getGoodsName, goodsName);
        orderLambdaQueryWrapper.like(goodsType != null, Goods::getGoodsType, goodsType);


        Page<Goods> page = goodsService.page(orderPage, orderLambdaQueryWrapper);


        return new Result(Code.success, "", page);

    }

    /**
     * 用户查询商品
     *
     * @param current
     * @param size
     * @return
     */
    @GetMapping("u")
    public Result select(@RequestParam("current") Integer current, @RequestParam("size") Integer size
    ) {


        LambdaQueryWrapper<Goods> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Goods> orderPage = new Page<>(current, size);
        Page<Goods> page = goodsService.page(orderPage, orderLambdaQueryWrapper);

        return new Result(Code.success, "", page);

    }

    /**
     * 添加商品
     *
     * @param request
     * @param goods
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PostMapping
    public Result add(HttpServletRequest request, @RequestParam("goods") String goods, @RequestParam MultipartFile multipartFile) throws IOException {


        Goods goods1 = JSON.parseObject(goods, Goods.class);
        byte[] bytes = multipartFile.getInputStream().readAllBytes();
        Integer loginBusiness = JwtTokenUntil.getClaim(request, "loginBusiness").asInt();
        goods1.setGoodsBusinessId(loginBusiness);
        goods1.setGoodsImg(bytes);
        boolean save = goodsService.save(goods1);
        return new Result(Code.success, "", save);
    }

    /**
     * 删除商品
     *
     * @param goods
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestBody Goods goods) {

        boolean b = goodsService.removeById(goods);
        return new Result(Code.success, "", b);

    }

    /**
     * 修改商品包括图片
     *
     * @param goods
     * @param multipartFile
     * @return
     * @throws IOException
     */
    @PutMapping
    private Result update(@RequestParam("goods") String goods, @RequestParam MultipartFile multipartFile) throws IOException {

        Goods goods1 = JSON.parseObject(goods, Goods.class);
        byte[] bytes = multipartFile.getInputStream().readAllBytes();
        goods1.setGoodsImg(bytes);

        boolean b = goodsService.updateById(goods1);
        return new Result(Code.success, "", b);

    }

    /**
     * 修改商品不包括图片
     *
     * @param goods
     * @return
     * @throws IOException
     */
    @PutMapping("edit")
    private Result update(@RequestParam("goods") String goods) throws IOException {

        Goods goods1 = JSON.parseObject(goods, Goods.class);
        boolean b = goodsService.updateById(goods1);
        return new Result(Code.success, "", b);

    }


}
